﻿using System.Diagnostics;

namespace CodeGenerator
{

    [DebuggerDisplay("Code={Code},Message={Message},Data={Data}")]
    public class FuncResult<T> : FuncResult
    {
        /// <summary>
        /// 返回的内容
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        ///  T data 隐式转换 FuncResult<T>.SuccessResult(data)
        /// </summary>
        /// <param name="data"></param>
        public static implicit operator FuncResult<T>(T data)
        {
            return FuncResult.Success(data);
        }
    }



    [DebuggerDisplay("Code={Code},Message={Message}")]
    public class FuncResult
    {

        public const string _FAIL_CODE = "0500";


        public const string _SUCCESS_CODE = "0000";


        public const string _SUCCESS_MESSAGE = "操作成功";

        public string Code { get; set; }

        public string Message { get; set; }

        public static FuncResult<T> Success<T>(T content, string message = null)
        {
            return new FuncResult<T>
            {
                Data = content,
                Code = "0000",
                Message = (message ?? "操作成功")
            };
        }

        public static FuncResult Success(string message = null)
        {
            return new FuncResult
            {
                Code = "0000",
                Message = (message ?? "操作成功")
            };
        }


        public static FuncResult<T> Fail<T>(T data, string message, string code = "0500")
        {
            return new FuncResult<T>
            {
                Data = data,
                Message = message,
                Code = code
            };
        }


        public static FuncResult<T> Fail<T>(string message, string code = "0500")
        {
            return Fail(default(T), message, code);
        }


        public static FuncResult Fail(string message, string code = "0500")
        {
            return new FuncResult
            {
                Message = message,
                Code = code
            };
        }

        public static implicit operator bool(FuncResult result)
        {
            if (result != null)
            {
                return result.Code == "0000";
            }

            return false;
        }
    }
}
